﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using DTO;
using BUS;

public partial class ChiTietGianHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ID = Request.QueryString["ID"];
            GianHangDTO gianhang = GianHangBUS.LayGianHangTheoID(int.Parse(ID));
            HienThongTinGianHang(gianhang);

            List<SanPhamThuongDangBanTrenGianHangDTO> listspthuong = SanPhamThuongDangBanTrenGianHangBUS.LayDanhSachSanPhamThuongTheoMaGianHang(gianhang.ID);
            List<SanPhamDTO> listSP = new List<SanPhamDTO>();
            for (int i = 0; i < listspthuong.Count; i++)
            {
                listSP.Add(SanPhamBUS.LaySanPhamTheoID(listspthuong[i].MaSanPham));
            }

            lbThongTinTrinhBay.Text = TaoBang(listSP);
        }

       
    }

    private void HienThongTinGianHang(GianHangDTO gianhang)
    {
        ThanhVienDTO chugianhang = ThanhVienBUS.LayThanhVienTheoID(gianhang.ChuGianHang);
        lbDiaChi.Text = gianhang.DiaChi;
        lbDienThoai.Text = gianhang.SoDienThoai;
        lbDoanhThuBanDuoc.Text = gianhang.DoanhSoBanDuoc.ToString();
        lbNgayMoGianHang.Text = gianhang.NgayMoGianHang.ToShortDateString();
        lbSoLuongTruyCap.Text = gianhang.SoLuongTruyCap.ToString();
        lbTenChuGianHanng.Text = chugianhang.TenThanhVien;
        lbTenGianHang.Text = gianhang.TenGianHang;
        imgLoGoGianHang.ImageUrl = gianhang.ThongTinTrinhBay;
    }

    private string TaoBang(List<SanPhamDTO> listSP)
    {
        string table = "<table  cellspacing='0' border='0' style='border-collapse:collapse;' width='100%' text-align='center'>";
        int sodong = listSP.Count / 3;
        if (sodong > 0)
        {
            for (int i = 0; i < sodong; i++)
            {
                table += TaoDong(listSP, i * 3);
            }
        }
        else
        {
            table += TaoDong(listSP, 0);
        }
        table += "</table>";
        return table;
    }

    private string TaoDong(List<SanPhamDTO> listSP, int index)
    {
        string result = "<tr>";
        result += TaoCot(listSP, index);
        return result;
    }

    private string TaoCot(List<SanPhamDTO> listSP, int index)
    {
        String shopID = Request["ID"];
        string result = "";
        for (int i = index; i < listSP.Count; i++)
        {
            result += "<td width='33%'> <a href='";
            if (SanPhamBUS.KiemTraSanPhamLaLapTop(listSP[i].ID))
                result += "ChiTietLapTop.aspx?ID=" + listSP[i].ID.ToString() + "'";
            else
                result += "ChiTietDienThoai.aspx?ID=" + listSP[i].ID.ToString() + "'";
            result += " style='color:#0000CC;font-size:Medium;font-weight:bold;'>";
            result += listSP[i].TenSanPham + "</a> <br/><br/>";
            result += "<img src='Image/" + ParseDuongDanHinh(listSP[i].HinhSanPham) + "' ";
            result += "style='height:150px;width:175px;border-width:0px;' /><br/><br/>";
            result += "<span style='color:Red; font-size:Medium;font-weight:bold;'>Giá bán hiện tại : " + listSP[i].GiaBanHienTai.ToString() +
                "  VNĐ</span>";
            result += " <a href='GioHang.aspx?itemID=" + listSP[i].ID.ToString() + "&shopID="+shopID + "'";
            result += "style='display:inline-block;height:18px;'>";
            result += "<img src='images/index_56.gif' style='border-width:0px;' /></a><br/><br/> </td>";
        }
        return result;
    }

    private string ParseDuongDanHinh(string s)
    {
        return s.Replace(" ", "%20");
        
    }

}
